﻿using SandBox.Missions.MissionLogics;
using SandBox.View.Missions;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.Core;
using TaleWorlds.MountAndBlade;
using TaleWorlds.MountAndBlade.GauntletUI.Mission;
using TaleWorlds.MountAndBlade.Source.Missions;
using TaleWorlds.MountAndBlade.View;
using TaleWorlds.MountAndBlade.View.MissionViews;

namespace MarriageFertility.Missions;

public static class DuelMissions
{
    private static MissionInitializerRecord CreateDuelMissionInitializerRecord(
        string sceneName,
        string sceneLevels = "",
        bool doNotUseLoadingScreen = false)
    {
        return new MissionInitializerRecord(sceneName)
        {
            DamageToPlayerMultiplier = Campaign.Current.Models.DifficultyModel.GetDamageToPlayerMultiplier(),
            DamageToFriendsMultiplier =
                Campaign.Current.Models.DifficultyModel.GetPlayerTroopsReceivedDamageMultiplier(),
            PlayingInCampaignMode = Campaign.Current.GameMode == CampaignGameMode.Campaign,
            AtmosphereOnCampaign =
                Campaign.Current.Models.MapWeatherModel.GetAtmosphereModel(MobileParty.MainParty.GetLogicalPosition()),
            SceneLevels = sceneLevels,
            DoNotUseLoadingScreen = doNotUseLoadingScreen
        };
    }

    public static Mission OpenDuelMission(
        string scene,
        string sceneLevels,
        bool spawnBothSidesWithHorse,
        bool friendlyDuel,
        bool isInsideSettlement)
    {
        return MissionState.OpenNew("DuelMission", CreateDuelMissionInitializerRecord(scene, sceneLevels),
            a => new MissionBehavior[]
            {
                new MissionCampaignView(),
                new CampaignMissionComponent(),
                new MissionOptionsComponent(),
                new DuelSingleMissionLogic(spawnBothSidesWithHorse, friendlyDuel, isInsideSettlement),
                ViewCreator.CreateMissionSingleplayerEscapeMenu(CampaignOptions.IsIronmanMode),
                ViewCreator.CreateMissionAgentStatusUIHandler(a),
                ViewCreator.CreateMissionMainAgentEquipmentController(a),
                new MissionSingleplayerViewHandler(),
                isInsideSettlement ? new MissionAudienceHandler(0.4f + MBRandom.RandomFloat * 0.3f) : null,
                new MissionBoundaryWallView(),
                new MissionItemContourControllerView(),
                new MissionAgentContourControllerView(),
                new MissionGauntletOptionsUIHandler(),
                new AgentHumanAILogic(),
                ViewCreator.CreateOptionsUIHandler(),
                ViewCreator.CreateMissionLeaveView(),
                ViewCreator.CreateSingleplayerMissionKillNotificationUIHandler(),
                ViewCreator.CreatePhotoModeView()
            });
    }

    public static Mission OpenMultipleMission(
        string scene,
        string sceneLevels,
        bool spawnBothSidesWithHorse,
        bool friendlyDuel,
        bool isInsideSettlement)
    {
        return MissionState.OpenNew("DuelMultipleMission", CreateDuelMissionInitializerRecord(scene, sceneLevels),
            a => new MissionBehavior[]
            {
                new MissionCampaignView(),
                new CampaignMissionComponent(),
                new MissionOptionsComponent(),
                new DuelMultipleMissiionLogic(spawnBothSidesWithHorse, friendlyDuel, isInsideSettlement),
                ViewCreator.CreateMissionSingleplayerEscapeMenu(CampaignOptions.IsIronmanMode),
                ViewCreator.CreateMissionAgentStatusUIHandler(a),
                ViewCreator.CreateMissionMainAgentEquipmentController(a),
                new MissionSingleplayerViewHandler(),
                isInsideSettlement ? new MissionAudienceHandler(0.4f + MBRandom.RandomFloat * 0.3f) : null,
                new MissionBoundaryWallView(),
                new MissionItemContourControllerView(),
                new MissionAgentContourControllerView(),
                new MissionGauntletOptionsUIHandler(),
                new AgentHumanAILogic(),
                ViewCreator.CreateOptionsUIHandler(),
                ViewCreator.CreateMissionLeaveView(),
                ViewCreator.CreateSingleplayerMissionKillNotificationUIHandler(),
                ViewCreator.CreatePhotoModeView()
            });
    }
    
}